<?php
namespace Shopmodule\Controller;
use Think\Controller;
class JifenController extends ExtendController {
	public function dddhjf(){
		$usernick = (I("usernick"));
		$sid = I("sid");
		$oid = I("oid");
		$sign = I("sign");
		$ts = I("ti");
		if($usernick && (md5($sid."|".$oid."|".$usernick."|".C("FRONT_SECRET").$ts)==$sign) && (time()-$ts<3600)){
			if(!$oid){
				$data["msg"]="抱歉，必须输入您的订单号哦！";
				$data["login"]=false;
				$this->ajaxReturn($data);
				die();
			}
			$usernick = urldecode_utf8($usernick);
			$TOOL = M("ToolsStatus");
			$cfg = json_decode($TOOL->where("`sid`=".$sid." AND `tool_id`=13")->getField("`cfg`"),true);
			if(!$cfg["dddhjf"] || !$cfg["onoff_dddhjf"]){
				$data["msg"]="抱歉，现在无法用订单兑换积分。";
				$data["login"]=false;
				$this->ajaxReturn($data);
				die();
			}
			$SHOP = M("ShopCfg");
			$sessionkey = $SHOP->where("`sid`=".$sid)->getField("sessionkey");
			if(!$sessionkey){
				$data["msg"]="抱歉，无法读取订单信息，请联系客服！";
				$data["login"]=false;
				$this->ajaxReturn($data);
				die();
			}
			$ORD = M("SeedsOrders");
			$id = $ORD->where("`oid`=".$oid)->getField("`id`");
			if($id){
				$data["msg"]="抱歉，这个订单号已经兑换过了哦！";
				$data["login"]=false;
				$this->ajaxReturn($data);
				die();
			}
			Vendor('taobaosdk.top.request.TradeGetRequest');
			$req = new \TradeGetRequest;
			$req->setFields("status,payment,pay_time,buyer_nick,seller_nick,consign_time");
			$req->setTid($oid);
			// $sessionkey = '6100022d76babbab2f3cfbb2320c7011fb39887c02d21de1864258896';
			// 1111820015955505
			// 1110956187655505
			$resp = $this->c->execute($req, $sessionkey);
			if($resp->code){
				$data["msg"]="抱歉，读取订单错误，请联系客服。错误代码：".$resp->code.",".$resp->sub_code."：".$resp->msg.",".$resp->sub_msg;
				$this->ajaxReturn($data);
				die();
			}
			if((string)$resp->trade->buyer_nick!=$usernick){
				$data["msg"]="抱歉，这个订单号不是您的！请重新输入。";
				$this->ajaxReturn($data);
				die();	
			}
			// if((string)$resp->trade->status != "TRADE_FINISHED"){
				// $data["msg"]="抱歉，只有交易完成的订单号才可以兑换积分哦！";
				// $this->ajaxReturn($data);
				// die();
			// }
			$payment = (float)$resp->trade->payment;
			$dhjf = 0;
			foreach($cfg["dddhjf"] as $k=>$ddjf){
				$dddh[$ddjf["ddje"]]=$ddjf["dhjf"];
			}
			ksort($dddh);
			foreach($dddh as $je=>$jf){
				if($payment >= $je){
					$dhjf = $jf;
				}
				$dhmsg .= "满".$je."元兑换".$jf."积分";
			}
			if($dhjf<=0){
				$data["msg"]="抱歉，您的订单号不符合兑换条件。".$dhmsg."不累计，不翻倍";
				$this->ajaxReturn($data);
				die();
			}
			
			$USR = D("User");
			$USR->usernick=$usernick;
			$USR->sid=$sid;
			$USR->getShopUser();
			
			// 保存获取记录
			$Seeds = M("Seeds");
			$da["uzuid"]=$USR->suid;
			$da["quangity"]=$dhjf;
			$da["nextseeds"]=0;
			$da["addtime"]=date("Y-m-d H:i:s",time());
			$da["addby"]=1;
			$da["type"]=1;
			$da["reason"]=C("SEEDS_REASON")["dddh"]["id"];
			$da["status"]=1;
			$da["sid"]=$sid;
			$sre = $Seeds->add($da);
			if(!$sre){
				$data["msg"]="抱歉，兑换失败。请联系客服。错误代码：BCHQJL";
				$this->ajaxReturn($data);
				die();
			}
			
			// 保存订单号
			// $JORD = M("SeedsOrders");
			$data["sid"] = $sid;
			$data["user_id"] = 0;
			$data["user_nick"] = $resp->trade->seller_nick;
			$data["buyer_nick"] = $resp->trade->buyer_nick;
			$data["payment"] = $resp->trade->payment;
			$data["oid"] = $oid;
			$data["tid"] = $oid;
			// $data["type"] = 
			$data["pub_time"] = $resp->trade->consign_time?$resp->trade->consign_time:date("Y-m-d H:i:s");
			$data["topic"] = $resp->trade->status;
			// $data["trade_type"] = 
			// $data["status"]
			// $data["rate_level"]
			$data["ts"] = date("Y-m-d H:i:s");
			$data["seeds_id"] = $sre;
			$data["seeds"] = $dhjf;
			$jre = $ORD->add($data);
			if(!$jre){
				$data["msg"]="抱歉，兑换失败。请联系客服。错误代码：BCDDH";
				$this->ajaxReturn($data);
				die();
			}
			// 为用户增加积分，累计购物总额
			unset($data);
			$data['seeds'] = array('exp','seeds+'.$dhjf);
			$data['sumpay'] = array('exp','sumpay+'.($resp->trade->payment*100));
			$EU = M("ShopUsers");
			$ere = $EU->where("`id`=".$USR->suid)->save($data);
			// $ere = $EU->where("`id`=".$USR->suid)->setInc('seeds',$dhjf);
			if(!$ere){
				$data["msg"]="抱歉，兑换失败。请联系客服。错误代码：WYHZJJF";
				$this->ajaxReturn($data);
				die();
			}
			$data["msg"]="亲，您已成功获得".$dhjf."积分。积分大奖正和你招手啦！";
		}else{
			$data["msg"]="抱歉，您等待的时间太长。请刷新一下页面重试。";
			$data["login"]=false;
		}
		$this->ajaxReturn($data);
	}
	// 签到加成在前台提示用户
	public function jfjc(){
		$usernick = urldecode_utf8(I("usernick"));
		$sid = I("sid");
		$TOOL = M("ToolsStatus");
		$cfg = json_decode($TOOL->where("`sid`=".$sid." AND `tool_id`=13")->getField("`cfg`"),true);
		if(!$cfg || !$cfg["jc"] || !$cfg["onoff_qdjc"]){
			$data["msg"]="抱歉，现在无法用积分加成，详情请联系客服";
			$data["login"]=false;
			$this->ajaxReturn($data);
			die();
		}
		$USR = D("User");
		$USR->usernick=$usernick;
		$USR->sid=$sid;
		$USR->getShopUser();
		$ljdd=$USR->getUserSumpay();
		$ewjf=0;
		foreach($cfg["jc"] as $k=>$jc){
			$jfjc[$jc["jine"]]= $jc["jifen"];
		}
		ksort($jfjc);
		foreach($jfjc as $je => $jc){
			if($ljdd>=$je){
				$ewjf=$jc;
			}
			$msg .= "兑换".$je."元订单，每天签到可额外获得".$jc."积分。";
		}
		if($ewjf>0){
			$data["msg"]="您已兑换".$ljdd."元订单，每天签到可额外获得".$ewjf."积分。".$msg."订单换积分，越换积分越多哦！<input class=\"btn btn-mini btn-info js-dhdh\" type=\"button\" value=\"兑换\">";
		}else{
			$data["msg"]="抱歉，您只兑换了".$ljdd."元订单，还不能额外获得签到加分。".$msg."订单换积分，越换积分越多哦！<input class=\"btn btn-mini btn-info js-dhdh\" type=\"button\" value=\"兑换\">";
		}
		$this->ajaxReturn($data);
	}
	// 指定日期内补签
	public function buqian(){
		$usernick = I("usernick");
		$sid = I("sid");
		$curdate = I("curdate");
		$sign = I("sign");
		$ts = I("ti");
		if($usernick && (md5($sid."|".$curdate."|".$usernick."|".C("FRONT_SECRET").$ts)==$sign) && (time()-$ts<3600)){
			$TOOL = M("ToolsStatus");
			$cfg = json_decode($TOOL->where("`sid`=".$sid." AND `tool_id`=13")->getField("`cfg`"),true);
			if(!$cfg["onoff"]){
				$data["msg"]="抱歉，现在无法使用签到功能，详情请联系客服";
				$data["login"]=false;
				$this->ajaxReturn($data);
				die();
			}
			$usernick =urldecode_utf8($usernick);
				$USR = D("User");
				$USR->usernick=$usernick;
				$USR->sid=$sid;
				$USR->buqianday=$curdate;
				$data = $USR->buqian();
		}else{
			$data["msg"]="抱歉，您停留的时间太长，请刷新重试";
		}
		$this->ajaxReturn($data);
	}
	// 前台显示签到日历，需配合js使用。
	public function qiandaorili(){
		$usernick = urldecode_utf8(I("usernick"));
		$sid = I("sid");
		$curdate = I("curdate");
		$Seeds = D("User");
		$Seeds->usernick = $usernick;
		$Seeds->sid = $sid;
		$Seeds->curdate = $curdate;
		$Seeds->rili();
		$signlist = $Seeds->signlist;
		$data["signlist"]=$signlist;
		// $data["signlist"]=json_decode('[{"signDay":29,"signType":1},{"signDay":2,"signType":2},{"signDay":10,"signType":3},{"signDay":4,"signType":4},{"signDay":5,"signType":5}]');
		$this->ajaxReturn($data);
	}
	public function read_dhjp(){
		// 读取兑换奖品及条件
		$usernick = (I("usernick"));
		$sid = I("sid");
		$top_sign = I("top_sign");
		$sign = I("sign");
		$ts = I("ti");
		if($usernick && (md5($sid."|".$usernick."|".$top_sign."|".C("FRONT_SECRET").$ts)==$sign) && (time()-$ts<3600)){
			$CFG = M("ToolsStatus");
			$cfg = json_decode($CFG->where("`tool_id`=13 AND `sid`=".$sid)->getField("cfg"),true);
			if(!$cfg["onoff_jfdh"]){
				$data["status"]=false;
				$data["msg"]="抱歉，现在不能兑换奖品。";
				$this->ajaxReturn($data);
				die();
			}
			foreach($cfg["dh"] as $dh){
				$gids[]= $dh["gift"];
			}
			$GFT = M("EventsGifts");
			$gifts = $GFT->where("`id` IN (".implode($gids,",").") AND `sid`=".$sid)->getField("`id`,`name`,`pic_url`");
			foreach($cfg["dh"] as $k=>$dh){
				foreach($gifts as $j=>$gift){
					if($dh["gift"]==$gift["id"]){
						$cfg["dh"][$k]["gift_name"]=$gift["name"];
						$cfg["dh"][$k]["gift_pic_url"]=$gift["pic_url"];
					}
				}
				if($dh["gift"]==0){
					unset($cfg["dh"][$k]);
				}
			}
			$data["duihuan"]=array_merge($cfg["dh"]);
		}else{
			$data["status"]=false;
			$data["msg"]="您停留的时间太久，请刷新页面";
		}
		$this->ajaxReturn($data);
	}
	public function qiandao(){
		$usernick = (I("usernick"));
		$sid = I("sid");
		$top_sign = I("top_sign");
		$sign = I("sign");
		$ts = I("ti");
		if($usernick && (md5($sid."|".$usernick."|".$top_sign."|".C("FRONT_SECRET").$ts)==$sign) && (time()-$ts<3600)){
			// $QD = D("Seeds");
			// $QD->usernick = $usernick;
			// $QD->sid = $sid; 
			// $data = $QD->qiandao();
			$TOOL = M("ToolsStatus");
			$cfg = json_decode($TOOL->where("`sid`=".$sid." AND `tool_id`=13")->getField("`cfg`"),true);
			if(!$cfg["onoff"]){
				$data["msg"]="抱歉，现在无法使用签到功能，详情请联系客服";
				$data["status"]=false;
				$this->ajaxReturn($data);
				die();
			}
			$Seeds = D("User");
			$Seeds->usernick = urldecode_utf8($usernick);
			$Seeds->sid = $sid; 
			$data = $Seeds->qiandao();
		}else{
			$data["status"]=false;
		}
		
		$this->ajaxReturn($data);
	}
	public function showdaysdh(){
		// 列出连续签到兑换奖品
		$usernick = (I("usernick"));
		$gids = I("gids");
		$sid = I("sid");
		$sign = I("sign");
		$ts = I("ti");
		if($usernick && (md5($usernick."|".$sid."|".$gids."|".C("FRONT_SECRET").$ts)==$sign) && (time()-$ts<3600)){
			$gids = explode(",",$gids);
			$TOOL = M("ToolsStatus");
			$cfg = json_decode($TOOL->where("`sid`=".$sid." AND `tool_id`=13")->getField("`cfg`"),true);
			if(!$cfg["onoff_qddh"]){
				$data["msg"]="抱歉，现在无法兑换奖品，详情请联系客服";
				$data["status"]=false;
				$this->ajaxReturn($data);
				die();
			}
			$allowdh = false;
			foreach($cfg["qddh"] as $qddh){
				foreach($gids as $gid){
					if($gid == $qddh["gift"]){
						$allowdh = $qddh;
					}
				}
			}
			if($allowdh){
				$GFT = M("EventsGifts");
				$allowdh["gift"] = current($GFT->where("`sid`=".$sid." AND `id` IN (".implode(",",$gids).")")->getField("`id`,`name`,`pic_url`"));
				$allowdh["status"]=true;
			}else{
				$allowdh["status"]=false;
				$allowdh["msg"]="抱歉，不能领取这个奖品";
			}
		}else{
			$allowdh["status"]=false;
			$allowdh["msg"]="抱歉，您停留的时间太久，请刷新页面";
		}
		$this->ajaxReturn($allowdh);
	}
	public function daysduih(){
		// 连续签到兑换奖品		
		$usernick = (I("usernick"));
		$gids = I("gids");
		$sid = I("sid");
		$sign = I("sign");
		$date = I("date");
		$ts = I("ti");
		if($usernick && (md5($usernick."|".$sid."|".$gids."|".$date."|".C("FRONT_SECRET").$ts)==$sign) && (time()-$ts<3600)){
			
			$CFG = M("ToolsStatus");
			$cfg = json_decode($CFG->where("`tool_id`=13 AND `sid`=".$sid)->getField("cfg"),true);
			if(!$cfg["onoff_qddh"]){
				$data["status"]=false;
				$data["msg"]="抱歉，签到兑换已关闭";
				$this->ajaxReturn($data);
				die();
			}
			
			$U = D("User");
			$U->usernick = urldecode_utf8($usernick);
			$U->sid = $sid;
			$U->curdate = date("Y-m-d");
			$U->rili();
			$signlist = $U->signlist;
			
			$isok = false;
			foreach($signlist as $list){
				if($list["date"]==date("Y-m-d",strtotime($date)) && ($list["signType"]==1||$list["signType"]==4) ){
					$isok = true;
					$gid = $list["giftId"];
					$qdinfo = $list;
				}
			}
			$msg = $isok?"允许兑换。":"亲，您需要签到才可以领取奖品哦！";
			if(!$isok){
				$data["status"]=false;
				$data["msg"]=$msg;
				$this->ajaxReturn($data);
				die();
			}
			$U->getShopUser();
			$DHINF = M("SeedsQddh");
			$dhid = $DHINF->where("`sid`=".$sid." AND `suid`=".$U->suid." AND DATE_FORMAT(`qd_date`,'%Y-%m-%d')='".$qdinfo["date"]."'")->getField("`id`");
			if($dhid){
				$data["status"]=false;
				$data["msg"]="亲，您已经兑换过这天的奖品啦，请不要重复兑换哦！";
				$this->ajaxReturn($data);
				die();
			}
			//*****
			$GFT = M("EventsGifts");
			$gift = current($GFT->where("`id` = (".$gid.") AND `sid`=".$sid." AND `status`=0")->getField("`id`,`name`,`type`,`quantity`,`type`,`level`,`num_iids`,`tagid`"));
			if($gift && $gift["quantity"]>0){
				if($U->suid){
					$UG = D("Shopadmin/UsersGifts");
					$UG->sid=$sid;
					$UG->UserSPID=$U->suid;
					$UG->giftid=$gid;
					$UG->gifttp=$gift["type"];
					$UG->gifttagid=$gift["tagid"];
					$UG->getway='qddh';
					$UG->send_gift();
					if($UG->status["status"]==1){
						$GFT->where("`id`=".$gid)->setDec('quantity'); //数量减1
						// 保存兑换记录
						$data = Array();
						$data["sid"]=$sid;
						$data["gift_id"]=$gid;
						$data["user_nick"]=$usernick;
						$data["suid"]=$U->suid;
						$data["user_gift_id"]=$UG->status["ugid"];
						$data["qd_date"]=$qdinfo["date"];
						$data["get_time"]=date("Y-m-d H:i:s");
						$DHINF->add($data);
						// $SUT = M("ShopUsers");
						// $SUT->where("`id`=".$User->suid)->setDec('seeds',$dhjf[$gid]); //用户积分减掉
						$data["status"]=true;
						$data["msg"]="兑换成功！请查看“我的奖品”获取领奖信息";
					}else{
						$data["status"]=false;
						$data["msg"]="兑换失败！请联系管理员，错误代码 QDDHFJ";
					}
				}else{
					$data["status"]=false;
					$data["msg"]="读取用户信息错误";
				}
			}else{
				if($gift["quantity"]<=0){
					$data["status"]=false;
					$data["msg"]="抱歉，奖品已经被兑换完了！";
				}else{
					$data["status"]=false;
					$data["msg"]="抱歉，您兑换的奖品不存在！";
				}
			}
			//*****
			$data["msg"]=$data["msg"].$msg;
		}else{
			$data["status"]=false;
			$data["msg"]="not ok";
		}
		$this->ajaxReturn($data);
	}
	public function duihuan(){
		// 正式兑换等待发奖
		$usernick = (I("usernick"));
		$sid = I("sid");
		$top_sign = I("top_sign");
		$sign = I("sign");
		$ts = I("ti");
		$gid = I("gid");
		if($usernick && (md5($sid."|".$usernick."|".$top_sign."|".C("FRONT_SECRET").$ts)==$sign) && ((time()-(int)($ts/1000))<3600)){
			// $SU = D("ShopUsersView");
			// $seeds = $SU->where("`usernick`='".$usernick."' AND sm_shop_users.sid=".$sid)->getField("seeds");
			$usernick = urldecode_utf8($usernick);
			$U = D("User");
			$U->usernick=$usernick;
			$U->sid=$sid;
			$U->getShopUser();
			$seeds=$U->getCurrentUserSeeds();
			$seeds = $seeds["seeds"];
			$CFG = M("ToolsStatus");
			$cfg = json_decode($CFG->where("`tool_id`=13 AND `sid`=".$sid)->getField("cfg"),true);
			if($cfg["onoff"]==0||!$cfg["onoff"]){
				$data["status"]=false;
				$data["msg"]="抱歉，积分兑换已关闭";
				$this->ajaxReturn($data);
				die();
			}
			foreach($cfg["dh"] as $dh){
				$gids[]=$dh["gift"];
				$dhjf[$dh["gift"]]= $dh["jifen"];
			}
			if(!empty($dhjf[$gid]) && $dhjf[$gid]<=$seeds){
				// 兑换开始
				$GFT = M("EventsGifts");
				$gift = current($GFT->where("`id` = (".$gid.") AND `sid`=".$sid." AND `status`=0")->getField("`id`,`name`,`type`,`quantity`,`type`,`level`,`num_iids`,`tagid`"));
				if($gift && $gift["quantity"]>0){
					// $User = D("User");
					// $U->usernick=$usernick;
					// $U->sid=$sid;
					// $U->getShopUser();
					if($U->suid){
						$UG = D("Shopadmin/UsersGifts");
						$UG->sid=$sid;
						$UG->UserSPID=$U->suid;
						$UG->giftid=$gid;
						$UG->gifttp=$gift["type"];
						$UG->gifttagid=$gift["tagid"];
						$UG->getway='jifen';
						$UG->send_gift();
						if($UG->status["status"]==1){
							$GFT->where("`id`=".$gid)->setDec('quantity'); //数量减1
							$SUT = M("ShopUsers");
							$SUT->where("`id`=".$U->suid)->setDec('seeds',$dhjf[$gid]); //用户积分减掉
							$data["status"]=true;
							$data["msg"]="兑换成功！请查看“我的奖品”获取领奖信息";
						}else{
							$data["status"]=false;
							$data["msg"]="兑换失败！请联系管理员，错误代码 JFDHFJ";
						}
					}else{
						$data["status"]=false;
						$data["msg"]="读取用户信息错误";
					}
				}else{
					if($gift["quantity"]<=0){
						$data["status"]=false;
						$data["msg"]="抱歉，奖品已经被兑换完了！";
					}else{
						$data["status"]=false;
						$data["msg"]="抱歉，您兑换的奖品不存在！";
					}
				}
			}else{
				$data["status"]=false;
				if((time()-(int)($ts/1000)>3600)){
					$data["msg"]="抱歉，您停留的时间太长，请刷新一下吧！".(time()-(int)($ts/1000));
				}elseif($dhjf[$gid]>$seeds){
					$data["msg"]="抱歉，您的积分不足哦！您只有".$seeds."个积分，兑换该奖品需要".$dhjf[$gid]."个积分哦！";
				}else{
					$data["msg"]="抱歉，不可以兑换这个奖品哦！";
				}
			}
		}else{
			$data["status"]=false;
			$data["msg"]="签名失败，请刷新一下";
			// $data["msg"]="签名失败".$usernick."|".(time()-(int)($ts/1000)).'|'.md5($sid."|".$usernick."|".$top_sign."|".C("FRONT_SECRET").$ts)."|".$sign;
		}
		$this->ajaxReturn($data);
	}
}